package Final;

import java.awt.*;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import javax.swing.*;

//首先实现登录界面
class Login {
    static ResultSet rs;
    static PreparedStatement pstmtt = null;


    //主函数入口
    /*public static void main(String args[]) {
        Login l=new Login();
    }*/

    public Login() {
        javax.swing.JFrame jf=new javax.swing.JFrame();


        jf.setTitle("登录界面");
        jf.setSize(340,500);
        jf.setDefaultCloseOperation(3);
        jf.setLayout(new FlowLayout());
        jf.setLocationRelativeTo(null);
        jf.setResizable(false);

        //选择布局类型
        java.awt.FlowLayout fl=new java.awt.FlowLayout(FlowLayout.CENTER,5,5);
        jf.setLayout(fl);

        //设置格式大小
        java.awt.Dimension dim1=new java.awt.Dimension(340,300);//图片大小
        java.awt.Dimension dim2=new java.awt.Dimension(50, 20);//标签的大小
        java.awt.Dimension dim3=new java.awt.Dimension(250, 30);//输入框的大小
        java.awt.Dimension dim4=new java.awt.Dimension(70, 40);//按钮的大小

        //添加图片
        javax.swing.ImageIcon icon=new javax.swing.ImageIcon("D:\\Code\\java\\课设\\背景.jpg");
        JLabel labpic=new JLabel(icon);//设置为标签
        labpic.setHorizontalAlignment(SwingConstants.CENTER);//居中显示
        labpic.setPreferredSize(dim1);
        jf.add(labpic);

        //添加“账号”标签
        JLabel labname=new JLabel();
        labname.setText("账号");
        labname.setPreferredSize(dim2);
        jf.add(labname);

        //添加账号输入框，并添加监控事件
        JTextField textname=new JTextField();
        textname.setPreferredSize(dim3);
        jf.add(textname);

        //添加“密码”标签
        JLabel labpassword=new JLabel();
        labpassword.setText("密码");
        labpassword.setPreferredSize(dim2);
        jf.add(labpassword);

        //添加密码输入框，并添加监控事件
        JPasswordField jp=new JPasswordField();
        jp.setPreferredSize(dim3);
        jf.add(jp);

        //添加一个登录按钮
        javax.swing.JButton button_login=new javax.swing.JButton();
        button_login.setText("登录");
        button_login.setPreferredSize(dim4);
        button_login.setBounds(10,300,300,40);
        jf.add(button_login);

        //添加注册按钮
        javax.swing.JButton button_enroll=new javax.swing.JButton();
        button_enroll.setText("注册");
        button_enroll.setPreferredSize(dim4);
        jf.add(button_enroll);

        jf.setVisible(true);

        //首先实例化登录按钮监听类的对象，并把登录界面中账号和密码输入框的对象传给它
        //LoginListener ll=new LoginListener(jf,textname,jp);
        //对当前窗体添加监听方法
        // button.addActionListener(ll);//监控按钮

        button_login.addActionListener(e -> {
            Connection conn = Sql.open();
            //System.out.println("事件发生");
            try {

                String sql = "select * from admin where Account = ?";
                pstmtt = conn.prepareStatement(sql);
                pstmtt.setString(1,textname.getText());
                rs = pstmtt.executeQuery();
                int flag = 0;
                while(rs.next()){
                    System.out.println("a");
                    if(jp.getText().equals(rs.getString("password") )) {
                        new Menu();
                        flag = 1;
                        jf.dispose();
                    }
                }
                if(flag == 0){
                    new Error_Login();
                }

            } catch (SQLException throwables) {
                throwables.printStackTrace();
            } finally {
                Sql.close(conn);
            }
        });
        button_enroll.addActionListener(e -> {
            new Enroll();

        });


    }

    public static void main(String[] args) {
        try {
            //CreateFile_Admin.Create_Admin();
            CreateFile_Student.Create_Student();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        new Login();
    }
}